package com.liu.manager.service.impl;

import com.liu.manager.bean.ResponseResult;
import com.liu.manager.entity.RolerAndPermission;
import com.liu.manager.mapper.RolerAndPermissionMapper;
import com.liu.manager.mapper.SyspermissionMapper;
import com.liu.manager.mapper.SysrolerMapper;
import com.liu.manager.service.RolerAndPermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RolerAndPermissionSvcImpl implements RolerAndPermissionService {

    @Resource
    RolerAndPermissionMapper rolerAndPermissionMapper;

    @Override
    public ResponseResult insertRolerAndPermission(RolerAndPermission rolerAndPermission) {
        if(rolerAndPermission.getPermissionId() <= 0){
            return ResponseResult.ERROR("权限id错误");
        }
        if(rolerAndPermission.getRolerId() <= 0){
            return ResponseResult.ERROR("角色id错误");
        }
        long id = rolerAndPermissionMapper.insertPermissionAndRoler(rolerAndPermission);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("保存成功");
        responseResult.setResult(rolerAndPermission);
        return responseResult;
    }

    @Override
    public ResponseResult deleteRolerAndPermission(long id) {
        if(id <= 0){
            return ResponseResult.ERROR("id错误");
        }
        rolerAndPermissionMapper.deletePermissionAndRoler(id);
        return ResponseResult.OK();
    }

    @Override
    public ResponseResult findByRolerId(long rolerId) {
        if(rolerId <= 0){
            return ResponseResult.ERROR("角色id错误");
        }
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("保存成功");
        responseResult.setResult(rolerAndPermissionMapper.findByRolerId(rolerId));
        return responseResult;
    }

    @Override
    public ResponseResult findByPermissionId(long permissionId) {
        if(permissionId <= 0){
            return ResponseResult.ERROR("角色id错误");
        }
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("保存成功");
        responseResult.setResult(rolerAndPermissionMapper.findByPermissionId(permissionId));
        return responseResult;
    }
}
